from app import models
from app.link import Link

class LinkQuery(object):
	def __init__(self):
		self.__query = models.Link().all()

	def filter_by_url(self, url):
		self.__query = self.__query.filter('url =', url)
		return self

	def filter_by_tags(self, *tags):
		"""All of the specified tags must match in order for the entity
		to be included in the result (logical AND)"""
		for tag in tags:
			self.__query = self.__query.filter('tags =', tag)
		return self

	def filter_by_attributes(self, **attributes):
		#!!! This would be way inefficient
		return self

	def filter_by_domain(self, domain):
		self.__query = self.__query.filter('domain =', domain)
		return self

	def filter_by_subdomains(self, domain):
#		self.filter_by_domain(domain)
		self.__query = self.__query.filter('superdomains =', domain)
		return self

	def run(self):
		"""Returns a list of Link objects"""
		links = list()
		for link in self.__query.run():
			links.append(Link(
							url=link.url, 
							owner=link.owner,
							link_data=link.link_data, 
							tags=link.tags, 
							attr_data=link.attr_data, 
							key=link.key()))
		return links

